<?php
require_once($AppUI->getSystemClass('dp'));		// use the dPFramework for easy inclusion of this class here
require_once($AppUI->getModuleClass('items'));
require_once($AppUI->getModuleClass('quotations'));

class CQuotation_lines extends CDpObject{

	var $qitem_id = null;
	var $qitem_quotation = null;
	var $qitem_customer  = null;
	var $qitem_item  = null;
	//var $qitem_code = null;
//	var $qitem_name = null;
//	var $qitem_type = null;
//	var $qitem_quantity_unit = null;
//	var $qitem_weight_unit = null;
//	var $qitem_volume_unit = null;
	var $qitem_remarks = null;
	var $qitem_apply_mode = 0;
	var $qitem_fixed_amount = null;
	var $qitem_figure_list = null;
	var $qitem_price_list = null;
	var $qitem_unit_list = null;
	var $qitem_vat_type = 0;
	var $qitem_cargo_type = null;
	var $qitem_container = null;
	var $qitem_method = null;
	var $qitem_shipping_type = null;
	var $qitem_place_from = null;
	var $qitem_place_to = null;
	var $qitem_truck_detention = null;
	var $qitem_quantity_from = null;
	var $qitem_quantity_to = null;
	var $qitem_unit = null;
	var $qitem_price = null;
	var $qitem_price_apply_mode = null;
	var $qitem_limit_declaration = null;
	var $qitem_extra_declaration = null;
	var $qitem_surchange1_apply_mode = null;
	var $qitem_surchange1_price = null;
	var $qitem_surchange2_condition = null;
	var $qitem_surchange2_apply_mode = null;
	var $qitem_surchange2_price = null;

	function CQuotation_lines(){
		$this->CDpObject('quotation_lines', 'qitem_id');
	}

	// overload the delete method of the parent class for adaptation for quotations's needs
	function deleteById($id){
		$sql = 'DELETE FROM quotation_lines WHERE qitem_id = ' . $id;
		if (!db_exec($sql)) {
			return db_error();
		} else {
			return NULL;
		}
	}

	/*
	 *	Name: getListByQuoteId
	 *
	 *	Param: $id
	 *
	 *  Description: Get all item of quotation
	 *
	 */
	function getListByQuoteId($uid, $id){
		$q  = new DBQuery;

		$q->addTable('quotation_lines');
		$q->addWhere('qitem_quotation = ' . $id);
		$q->innerJoin('companies', 'c', 'quotation_lines.qitem_customer = c.company_id');
		$q->innerJoin('items', 'i', 'quotation_lines.qitem_item = i.item_id');
		$q->addQuery('quotation_lines.*, i.item_name, i.item_code, i.item_vat_code, c.company_name');
		$sql = $q->prepare();
		$q->clear();
		return db_loadList($sql);
	}

	function countListByQuoteId($uid, $id){
		$q  = new DBQuery;

		$q->addTable('quotation_lines');
		$q->addWhere('qitem_quotation = ' . $id);
		$q->innerJoin('companies', 'c', 'quotation_lines.qitem_customer = c.company_id');
		$q->innerJoin('items', 'i', 'quotation_lines.qitem_item = i.item_id');
		$q->addQuery('count(quotation_lines.qitem_id) as count');
		$sql = $q->prepare();
		$q->clear();
		return db_loadList($sql);
	}

	function load($id) {
		parent::load($id);
		$item = new CItem();
		$item->load($this->qitem_item);

		$this->item_name = $item->item_name;
		$this->item_code = $item->item_code;
		$this->item_vat_code = $item->item_vat_code;

		$this->qitem_figure_list = array();
		$this->qitem_price_list = array();
		if(intval($id) > 0){
			$q = new DBQuery();
			$q->addTable('quotation_item_figures');
			// join with md_unit to get unit's name
			$q->leftJoin('md_unit', 'u', 'quotation_item_figures.qitem_unit = u.unit_id');
			$q->addWhere('qitem_id = '.$id);
			$q->addQuery('qitem_figure, qitem_price, qitem_unit, u.unit_name');
			$q->addOrder('qitem_figure, qitem_price');
			$sql = $q->prepare();
			foreach(db_loadList($sql) as $row) {
				$this->qitem_figure_list[] = $row['qitem_figure'];
				$this->qitem_price_list[] = $row['qitem_price'];
				$this->qitem_unit_list[] = $row['qitem_unit'];
			}
		}
	}

	function loadFigureList($id){
		$result = array();
		if(intval($id) > 0){
			$q = new DBQuery();
			$q->addTable('quotation_item_figures');
			// join with md_unit to get unit's name
			$q->leftJoin('md_unit', 'u', 'quotation_item_figures.qitem_unit = u.unit_id');
			$q->addWhere('qitem_id = '.$id);
			$q->addQuery('qitem_figure, qitem_price, qitem_unit, u.unit_name');
			$q->addOrder('unit_name, qitem_figure');
			$sql = $q->prepare();
			foreach(db_loadList($sql) as $row) {
				$result['qitem_figure_list'][] = $row['qitem_figure'];
				$result['qitem_price_list'][] = $row['qitem_price'];
				$result['qitem_unit_list'][] = $row['unit_name'];
			}
		}
		return $result;
	}

	/*
	 * Description:
	 * 	Override funtion store
	 * 		$_POST['xxx'] is array that was posted by browser
	 * 		Delete records that is related with quotation if exist
	 *
	 */
	function store() {
		$qitem_figure_list = $_POST['qitem_figure_list'];
		$qitem_price_list = $_POST['qitem_price_list'];
		$qitem_unit_list = $_POST['qitem_unit_list'];
		unset($this->qitem_figure_list);
		unset($this->qitem_price_list);
		unset($this->qitem_unit_list);
		parent::store();

		// Delete old records
		if(!is_null($this->qitem_id) and (intval($this->qitem_id) > 0)){
			$sql = "delete from quotation_item_figures where qitem_id = ". $this->qitem_id;
			db_exec($sql);
		}
		if (is_array($qitem_figure_list) && count($qitem_figure_list) > 0) {
			$q = new DBQuery();
			for($i = 0; $i<count($qitem_figure_list);  $i++) {
				if(0 < strlen($qitem_figure_list[$i])){
					$q->addTable('quotation_item_figures');
					$q->addInsert('qitem_id', $this->qitem_id);
					$q->addInsert('qitem_figure', currency2Double($qitem_figure_list[$i]));
					$q->addInsert('qitem_price', currency2Double($qitem_price_list[$i]));
					$q->addInsert('qitem_unit', $qitem_unit_list[$i]);
					$q->exec();
					$q->clear();
				}
			}
		}
	}

	function canDelete(&$msg) {
		// Load line
		$this->load($this->qitem_id);
		// Load quotation
		$quot = new CQuotations();
		$quot->quote_id = $this->qitem_quotation;
		if (!$quot->canDelete( $msg )) {
			$msg = "You can not delete a 'quotation line' that is member of Quotation's actived/completed";
			return false;
		}
		return true;
	}
	
	function bind($hash) {
		if (!parent::bind($hash)) {
			return false;
		}
		$qitem_price_apply_mode = dPgetParam($hash, 'qitem_price_apply_mode', 0);
		$qitem_surchange1_apply_mode = dPgetParam($hash, 'qitem_surchange1_apply_mode', 0);
		$qitem_surchange2_apply_mode = dPgetParam($hash, 'qitem_surchange2_apply_mode', 0);
		
		$this->qitem_price_apply_mode = $qitem_price_apply_mode == 1 || $qitem_price_apply_mode == 'true' ? TRUE : FALSE;
		$this->qitem_surchange1_apply_mode = $qitem_surchange1_apply_mode == 1 || $qitem_surchange1_apply_mode == 'true' ? TRUE : FALSE;
		$this->qitem_surchange2_apply_mode = $qitem_surchange2_apply_mode == 1 || $qitem_surchange2_apply_mode == 'true' ? TRUE : FALSE;
		return true;
	}
}
?>
